package com.nanfangzhe.shirodemo.service.impl;

import com.nanfangzhe.shirodemo.entity.UUser;
import com.nanfangzhe.shirodemo.mapper.UUserMapper;
import com.nanfangzhe.shirodemo.service.UUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author nanfangzhe
 * @since 2020-11-24
 */
@Service
public class UUserServiceImpl extends ServiceImpl<UUserMapper, UUser> implements UUserService {

	@Autowired
	private UUserMapper dao;

	@Override
	public UUser getBy(String email, String username, String password) {
		QueryWrapper<UUser> wrapper = new QueryWrapper<UUser>();
		if (email != null)
			wrapper.eq("email", email);
		if (username != null)
			wrapper.eq("username", username);
		if (password != null)
			wrapper.eq("password", password);
		return dao.selectOne(wrapper);
	}

	@Override
	public UUser getByUsername(String username) {
		QueryWrapper<UUser> wrapper = new QueryWrapper<UUser>();
		if (username != null)
			wrapper.eq("username", username);
		// UUser user = dao.selectOne(wrapper);
		return dao.selectOne(wrapper);
	}

	@Override
	public Boolean add(UUser user) {
		String password = user.getPassword();
		user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
		return dao.insert(user) == 1;
	}

	@Override
	public List<String> getRoles(int id) {
		return dao.getRoles(id);
	}
	
	@Override
	public List<String> getPerms(int id) {
		return dao.getPerms(id);
	}

}
